package com.beehoo.dcportal.admin.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.beehoo.dcportal.admin.dao.ThemeDao;
import com.beehoo.dcportal.common.base.BaseDao;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.ThemeBean;
import com.beehoo.dcportal.model.ThemeCodeBean;

public class ThemeDaoImpl extends BaseDao implements ThemeDao {

	private static final Logger logger = Logger.getLogger(ThemeDaoImpl.class);
			
	@Override
	public String createTheme(ThemeBean themeBean) throws BaseException {
		String themeId = null;
		try {
			themeId = (String) sqlMapClient.insert("admin-theme.createTheme",
					themeBean);
			logger.info("模板管理-创建模板|#[中心编号：" + themeBean.getStreetId()
					+ "],[主题布局：" + themeBean.getThLayout()
					+ "],[主题样式：" + themeBean.getThStyle()
					+ "],[主题背景图：" + themeBean.getThBgImg()
					+ "],[主题背景色：" + themeBean.getThBgColor()
					+ "],[中心logo：" + themeBean.getThLogo()
					+ "],[主题名称：" + themeBean.getThName()
					+ "],[创建人编号：" + themeBean.getCreatorId()
					+ "],[创建人名称：" + themeBean.getCreator()
					+ "],[备注：" + themeBean.getRemark()
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("模板管理-创建模板|#[中心编号：" + themeBean.getStreetId()
					+ "],[主题布局：" + themeBean.getThLayout()
					+ "],[主题样式：" + themeBean.getThStyle()
					+ "],[主题背景图：" + themeBean.getThBgImg()
					+ "],[主题背景色：" + themeBean.getThBgColor()
					+ "],[中心logo：" + themeBean.getThLogo()
					+ "],[主题名称：" + themeBean.getThName()
					+ "],[创建人编号：" + themeBean.getCreatorId()
					+ "],[创建人名称：" + themeBean.getCreator()
					+ "],[备注：" + themeBean.getRemark()
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.theme.create.error");
		}
		return themeId;
	}

	@Override
	public int delTheme(String themeId) throws BaseException {
		int ret = 0;
		try {
			ret = sqlMapClient.delete("admin-theme.delTheme", themeId);
			logger.info("模板管理-删除模板|#[模板编号：" + themeId
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("模板管理-删除模板|#[模板编号：" + themeId
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.theme.del.error");
		}
		return ret;
	}

	@Override
	public ThemeBean queryTheme(Map<String, Object> paramMap)
			throws BaseException {
		ThemeBean themeBean = null;
		try {
			themeBean = (ThemeBean) sqlMapClient.queryForObject(
					"admin-theme.queryTheme", paramMap);
			logger.info("模板管理-查询模板|#[中心编号：" + paramMap.get("streetId")
					+ "],[主题编号：" + paramMap.get("themeId")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("模板管理-查询模板|#[中心编号：" + paramMap.get("streetId")
					+ "],[主题编号：" + paramMap.get("themeId")
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.theme.query.error");
		}
		return themeBean;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ThemeBean> queryThemes(Map<String, Object> paramMap)
			throws BaseException {
		List<ThemeBean> themeLs = null;
		try {
			themeLs = sqlMapClient.queryForList("admin-theme.queryTheme", paramMap);
			logger.info("模板管理-查询模板列表|#[中心编号：" + paramMap.get("streetId")
					+ "],[主题编号：" + paramMap.get("themeId")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("模板管理-查询模板列表|#[中心编号：" + paramMap.get("streetId")
					+ "],[主题编号：" + paramMap.get("themeId")
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.theme.query.error");
		}
		return themeLs;
	}

	@Override
	public int updateTheme(ThemeBean themeBean) throws BaseException {
		int ret = 0;
		try {
			ret = sqlMapClient.update("admin-theme.updateTheme", themeBean);
			logger.info("模板管理-更新模板|#[中心编号：" + themeBean.getStreetId()
					+ "],[主题编号：" + themeBean.getThemeId()
					+ "],[主题布局：" + themeBean.getThLayout()
					+ "],[主题样式：" + themeBean.getThStyle()
					+ "],[主题背景图：" + themeBean.getThBgImg()
					+ "],[主题背景色：" + themeBean.getThBgColor()
					+ "],[中心logo：" + themeBean.getThLogo()
					+ "],[主题名称：" + themeBean.getThName()
					+ "],[创建人编号：" + themeBean.getCreatorId()
					+ "],[创建人名称：" + themeBean.getCreator()
					+ "],[备注：" + themeBean.getRemark()
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("模板管理-更新模板|#[中心编号：" + themeBean.getStreetId()
					+ "],[主题编号：" + themeBean.getThemeId()
					+ "],[主题布局：" + themeBean.getThLayout()
					+ "],[主题样式：" + themeBean.getThStyle()
					+ "],[主题背景图：" + themeBean.getThBgImg()
					+ "],[主题背景色：" + themeBean.getThBgColor()
					+ "],[中心logo：" + themeBean.getThLogo()
					+ "],[主题名称：" + themeBean.getThName()
					+ "],[创建人编号：" + themeBean.getCreatorId()
					+ "],[创建人名称：" + themeBean.getCreator()
					+ "],[备注：" + themeBean.getRemark()
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.theme.update.error");
		}
		return ret;
	}

	@Override
	public void setInUseTheme(Map<String, Object> paramMap1,
			Map<String, Object> paramMap2) throws BaseException {
		try {
			sqlMapClient.startTransaction();
			sqlMapClient.getCurrentConnection().setAutoCommit(false);
			sqlMapClient.update("admin-theme.setInUseTheme", paramMap1);
			sqlMapClient.update("admin-theme.setInUseTheme", paramMap2);
			sqlMapClient.getCurrentConnection().commit();
			logger.info("模板管理-使用模板|#[中心编号：" + paramMap2.get("streetId")
					+ "],[主题编号：" + paramMap2.get("themeId")
					+ "],成功!");
		} catch (Exception e) {
			try {
				sqlMapClient.getCurrentConnection().rollback();
			} catch (SQLException e1) {
				throw new BaseException("custom.messages.error.admin.operate.error");
			}
			logger.error("模板管理-使用模板|#[中心编号：" + paramMap2.get("streetId")
					+ "],[主题编号：" + paramMap2.get("themeId")
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.theme.use.error");
		} finally {
			try {
				sqlMapClient.getCurrentConnection().setAutoCommit(true);
				sqlMapClient.endTransaction();
			} catch (SQLException e) {
				throw new BaseException("custom.messages.error.admin.operate.error");
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ThemeCodeBean> queryThemeCode() throws BaseException {
		List<ThemeCodeBean> codeLs = null;
		try {
			codeLs = sqlMapClient.queryForList("admin-theme.queryThemeCode");
			logger.info("模板管理-查询模板CODE|成功!");
		} catch (SQLException e) {
			logger.error("模板管理-查询模板CODE|失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.theme.init.error");
		}
		return codeLs;
	}

}
